/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app.model;

import java.util.ArrayList;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;
import at.socialconference.app.connection.ConnectionHandler;
import at.socialconference.inter.model.Conference;

public class AConference extends Conference implements Parcelable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private APlace aPlace;
	public ATalk[] aTalks;
	
	public AConference(Conference c) {
		super.setId(c.getId());
		super.setDate_from(c.getDate_from());
		super.setDate_to(c.getDate_to());
		super.setDescription(c.getDescription());
		super.setName(c.getName());
		setAPlace(new APlace(c.getPlace()));
		super.setTalks(c.getTalks());
		
		if(c.getTalks()!=null){
			aTalks = new ATalk[c.getTalks().size()];
			for(int i=0;i<aTalks.length;i++){
				aTalks[i] = new ATalk(c.getTalks().get(i));
			}
		}
		
	}
	
	public AConference(Parcel in) {
		super.setId(in.readInt());
		super.setName(in.readString());
		super.setDate_from(new Date(in.readLong()));
		super.setDate_to(new Date(in.readLong()));
		super.setDescription(in.readString());
		setAPlace((APlace)in.readParcelable(APlace.class.getClassLoader()));
		
		Parcelable[] tmp = (Parcelable[]) in.readParcelableArray(ATalk.class.getClassLoader());
		if(tmp!=null){
			aTalks = new ATalk[tmp.length];
			for(int i=0;i<aTalks.length;i++){
				aTalks[i] = (ATalk)tmp[i];
				
			}
		}
	}

	@Override
	public APlace getPlace() {
		return aPlace;
	}

	public void setAPlace(APlace aPlace) {
		this.aPlace = aPlace;
	}
	
	public ArrayList<ATalk> getATalks(){
		ArrayList<ATalk> talks = new ArrayList<ATalk>();
		for(ATalk talk : aTalks){
			talks.add(talk);
		}
		return talks;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(getId());
		dest.writeString(getName());
		dest.writeLong(getDate_from().getTime());
		dest.writeLong(getDate_to().getTime());
		dest.writeString(getDescription());
		dest.writeParcelable(getPlace(), 0);
		dest.writeParcelableArray(aTalks, 0);
	}
	
	public static final Parcelable.Creator<AConference> CREATOR = new Parcelable.Creator<AConference>() 
	{ 
		public AConference createFromParcel(Parcel in) 
		{ 
			return new AConference(in); 
		} 

		public AConference[] newArray(int size) 
		{ 
			return new AConference[size]; 
		} 
	};

	public boolean isEnrolled() {
		return ConnectionHandler.participatesToConference(getId());
	}

	public int enroll() {
		return ConnectionHandler.enroll(getId());
	}

}
